<template>
	<view class="content">
		<view>
			<swiper class="swiper" indicator-dots autoplay circular>
				<swiper-item v-for="(item,index) in swiperlist" :key="index">
					<image class="img" :src="'https://test.yglicai.com/'+item.imgurl" mode="scaleToFill" @click="imgClick(item.link)"></image>
				</swiper-item>
			</swiper>
			<uni-notice-bar v-if="noticeList.length>0" scrollable="true" single="true" showIcon="true" :text="noticeText"></uni-notice-bar>
		</view>
		<view class="product">
			<uni-icons type="sound" size="20" /><text>产品预告</text>
		</view>
		<view>
			<scroll-view class="scroll-view_H" scroll-x="true" scroll-left="120">
				<view class="scroll-view-item_H" v-for="(item,index) in productList" :key="index">
					<image class="img" :src="'https://test.yglicai.com/'+item.imgurl" mode="scaleToFill"></image>
				</view>
			</scroll-view>
		</view>
		<view class="news">
			<view class="header">
				<text>新闻资讯</text>
				<navigator url="" class="link">更多></navigator>
			</view>
			<view class="news-item" v-for="(item,index) in newsList" :key="index">
				<view class="left">
					<text class="title">{{item.title}}</text>
					<text class="time">{{item.pubdate}}</text>
				</view>
				<view class="right">
					<image class="img" mode="scaleToFill" :src="'https://test.yglicai.com/'+item.imgurl"></image>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		uniNoticeBar,
		uniIcons
	} from '@dcloudio/uni-ui'
	export default {
		data() {
			return {
				swiperlist: [],
				noticeList: [],
				noticeText: "",
				productList: [],
				newsList: [],
			}
		},
		components: {
			uniNoticeBar,
			uniIcons
		},
		onLoad() {
			this.getSwiperList();
			this.getNoticeList();
			this.getProductList();
			this.getNewsList();
		},
		onShow() {
			// this.startLoopNotice();
		},
		onHide() {
			this.timer && clearInterval(this.timer);
		},
		methods: {
			imgClick(url) {
				// #ifdef H5
				window.open(url);
				// #endif
			},
			getSwiperList() {
				const phone = uni.getStorageSync('userphone') || "";
				this.$http.post("kingdom.kfat.get_doc_info_for_website", "V2.0", {
					"pageNumber": "1",
					"pageSize": "10",
					"typeno": "181",
					phone: phone
				}).then(data => {
					console.log(data)
					if (data.flag === '1' && data.items) {
						this.swiperlist = data.items;
					} else {

					}
				})
			},
			getNoticeList() {
				const phone = uni.getStorageSync('userphone') || "";
				this.$http.post("kingdom.kfat.get_spm_area_content_list", "V2.0", {
					pageNumber: "1",
					pageSize: "10",
					areaid: "1007",
					phone: phone
				}).then(data => {
					console.log(data)
					if (data.flag === '1' && data.items) {
						this.noticeList = data.items;
						// this.startLoopNotice();
						if (this.noticeList.length > 0) {
							this.noticeText = this.noticeList[0].title;
						}
					} else {

					}
				})
			},
			getProductList() {
				const phone = uni.getStorageSync('userphone') || "";
				this.$http.post("kingdom.kfat.get_spm_area_content_list", "V2.0", {
					pageNumber: "1",
					pageSize: "10",
					areaid: "7001",
					phone: phone
				}).then(data => {
					console.log('product', data)
					if (data.flag === '1' && data.items) {
						this.productList = data.items;
					} else {

					}
				})
			},
			getNewsList() {
				const phone = uni.getStorageSync('userphone') || "";
				this.$http.post("kingdom.kfat.get_spm_area_content_list", "V2.0", {
					pageNumber: "1",
					pageSize: "10",
					areaid: "1006",
					phone: phone
				}).then(data => {
					console.log('getNewsList', data)
					if (data.flag === '1' && data.items) {
						this.newsList = data.items.length > 3 ? data.items.slice(0, 3) : data.items;
					} else {

					}
				})
			},
			startLoopNotice() {
				if (this.noticeList.length > 0) {
					this.noticeTextIndex = 0;
					this.noticeText = this.noticeList[this.noticeTextIndex].title;
					this.timer = setInterval(() => {
						this.noticeTextIndex++;
						this.noticeText = this.noticeList[this.noticeTextIndex].title;
						if (this.noticeTextIndex === this.noticeList.length - 1) {
							this.noticeTextIndex = 0;
						}
					}, 1000 * 5)
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.swiper {
		height: 400rpx;
		color: #333333;
	}

	.swiper-item {
		display: block;
		height: 400rpx;
		line-height: 300rpx;
		text-align: center;
	}

	.img {
		width: 100%;
		height: 100%;
	}

	.product {
		display: flex;
		align-items: center;
		justify-content: center;

		>text {
			font-size: 30rpx;
			margin: 20rpx 10rpx;
		}
	}

	.scroll-view_H {
		white-space: nowrap;
		width: 100%;
	}

	.scroll-view-item_H {
		display: inline-block;
		width: 320rpx;
		height: 220rpx;
		border-radius: 5px;
		overflow: hidden;
		margin: 20rpx 20rpx;

		.img {
			width: 100%;
			height: 100%;
		}
	}

	.news {
		.header {
			display: flex;
			justify-content: space-between;
			font-size: 30rpx;
			padding: 0 40rpx;
			align-items: center;
			background-color: #FFFFFF;
		}

		.link {
			font-size: 24rpx;
			color: #666666;
		}
	}

	.news-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 40rpx;
		border-bottom: 1px solid #dedede;

		.left {
			display: flex;
			flex-direction: column;
			flex: 1;

			.title {
				font-size: 30rpx;
			}

			.time {
				font-size: 20rpx;
				color: #999999;
				margin-top: 40rpx;
			}
		}

		.right {
			width: 240rpx;
			height: 130rpx;
		}
	}
</style>
